package com.dhy;

import com.dhy.handler.ImgHandler;
import com.dhy.handler.LocalImgHandler;
import com.dhy.util.GitUtil;
import com.dhy.util.YamlUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * typora将本地图片重新设置为图床图片
 * </p>
 *
 * @author 大忽悠
 * @create 2022/5/26 20:01
 */
@Slf4j
public class TyporaImgManager {
    private static final YamlUtil yamlUtil = new YamlUtil("file.yml");
    public static final String prefix;
    public static final String dir;
    public static final String urlPrefix;
    public static final String gitLocalDir;
    public static final String PATTERN = "![image-";
    public static final String IGNORE_STR = "https://";
    private static final List<ImgHandler> imgHandlers=new ArrayList<>();

    static {
        dir = yamlUtil.get("file.dir");
        prefix = yamlUtil.get("file.prefix");
        gitLocalDir = yamlUtil.get("file.gitLocalDir");
        if (prefix == null || dir == null || gitLocalDir == null) {
            throw new IllegalArgumentException("prefix,dir或者gitLocalDir参数未进行配置");
        }
        urlPrefix = prefix + dir;
        imgHandlers.add(new LocalImgHandler());
    }

    public static void handle(String localFilePath, String branch, String commitMsg) {
        beforeHandle(localFilePath);
        File file = imgHandlers.get(0).getLocalFile();
        File tempFile = imgHandlers.get(0).getTempFile();
        try (
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                PrintStream print = new PrintStream(new FileOutputStream(tempFile))
        ) {
            //git open
            GitUtil.openGit(gitLocalDir);
            String line = null;
            while ((line = reader.readLine()) != null) {
                print.print(handle(line));
            }
            print.flush();
            //git add
            GitUtil.gitAdd(dir);
            //git commit
            GitUtil.gitCommit(commitMsg);
            //git push---报错: 401
            GitUtil.gitPush(branch);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            GitUtil.close();
        }
        afterHandle();
    }

    public static void beforeHandle(String localFilePath){
        for (ImgHandler handler : imgHandlers) {
            if(handler.preHandle(localFilePath)){
                return;
            }
        }
    }

    public static String handle(String line) throws IOException {
        for (ImgHandler handler : imgHandlers) {
            String res = handler.handle(line);
           if(res!=null||!res.equals("")){
               return res;
           }
        }
        return line;
    }

    public static void afterHandle(){
        for (ImgHandler handler : imgHandlers) {
            if(handler.afterHandle()){
                return;
            }
        }
    }

    public void  addHandler(ImgHandler handler){
        imgHandlers.add(handler);
    }

    public void  addHandler(ImgHandler handler,int index){
        imgHandlers.add(index,handler);
    }
}
